package com.zry.yard.view.address

import android.app.Activity
import android.graphics.drawable.PaintDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import com.zry.yard.R

class AddressPickerPop(activity: Activity, linearLayout: ViewGroup) {

    init {
        val popupWindow = PopupWindow(activity)
        val rootView = LayoutInflater.from(activity).inflate(R.layout.pop_address_picker, null, false)
        val addressView: AddressPickerView = rootView.findViewById(R.id.apvAddress)
        addressView.setOnAddressPickerSure { address, province, city, district, provinceCode, cityCode, districtCode ->
            if (listener != null) {
                listener!!.handleResult(address, province, city, district, provinceCode, cityCode, districtCode)
            }
            popupWindow.dismiss()
        }
        popupWindow.contentView = rootView
        popupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
        popupWindow.height = ViewGroup.LayoutParams.MATCH_PARENT

        //设置返回键点击关闭
        popupWindow.isFocusable = true
        popupWindow.setBackgroundDrawable(PaintDrawable())

        popupWindow.showAsDropDown(linearLayout)
    }

    private var listener: AddressListener? = null

    interface AddressListener {
        fun handleResult(address: String, province: String, city: String, district: String, provinceCode: String, cityCode: String, districtCode: String)
    }

    fun setListener(listener: AddressListener): AddressPickerPop {
        this.listener = listener
        return this
    }

}